package game.leon.war.gate;

import game.leon.engine.utils.LinkedNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * User: bianpeng
 * Date: 12-1-10
 * 消息消费, 在messageReciever的线程中发起, 交给consumer来处理, 每一个消息都用future来做掉
 */
@Component
public class GateConsumer {
    @Autowired
    private GateHandlerService gateHandlerService;
    private ExecutorService executorService = Executors.newSingleThreadExecutor();
    public void consume(LinkedNode<GateMessage> messageList) {
        while (messageList != null) {
            consume(messageList.getItem());
            messageList = messageList.getNext();
        }
    }
    private void consume(final GateMessage gateMessage) {
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                gateHandlerService.handle(gateMessage);
            }
        });
    }
}
